package com.ruoyi.libb.controller;

import com.ruoyi.libb.entity.Contract;
import com.ruoyi.libb.service.ContractService;
import com.ruoyi.libb.utils.Result;
import com.ruoyi.libb.utlis.SendmailUtli;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

@RestController
@RequestMapping("/contract")
@Api(description="合同接口")
public class ContractController {

    @Resource
    private ContractService contractService;

    @ApiOperation(value = "合同的列表")
    @RequestMapping("/contractList")
    public Result contractList(){


        return contractService.contractList();
    }

    @ApiOperation(value = "合同的添加")
    @RequestMapping("/contractAdd")
    public Result contractAdd(@RequestBody Contract contract){

        return contractService.contractAdd(contract);
    }

    @ApiOperation(value = "合同的修改")
    @RequestMapping("/contractUpdate")
    public Result contractUpdate(@RequestBody Contract contract){

        return contractService.contractUpdate(contract);
    }

    @RequestMapping("/contractDelete")
    public Result contractDelete(@ApiParam(name = "id", value = "产品ID", required = true) @RequestParam("id") Integer id){


        return contractService.contractDelete(id);
    }

    @ApiOperation(value = "合同的pdf")
    @RequestMapping("/pdf")
    public Result Pdf() throws Exception {

            return contractService.pdf();

    }

    @ApiOperation(value = "合同的邮件发送")
    @RequestMapping("/sendmail")
    public Result Sendmail(String sendmail)  {

        try {
            SendmailUtli.sendmail(sendmail);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return Result.success();
    }


/*    @Test
    public void testCreateDbTable(){
        //使用classpath下的activiti.cfg.xml中的配置创建processEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //创建数据库表
        //processEngine.getRepositoryService().createDeployment().addClasspathResource("activiti.cfg.xml").deploy();
        System.out.println(processEngine);
        System.out.println("创建数据库表成功");
    }*/
}
